<?php $time_start = microtime(); ?>
<html>
    <head>
        <title>Reader Happiness Indicator</title>
        <meta charset="utf-8">
    </head>
    <body>
        <?php
        require_once dirname(__FILE__) . '/Config.php';
        require_once dirname(__FILE__) . '/GaFetcher.php';
        require_once dirname(__FILE__) . '/GaDataQuery.php';
        require_once dirname(__FILE__) . '/Models/Token.php';
        require_once dirname(__FILE__) . '/google-api-php-client/src/Google_Client.php';
        require_once dirname(__FILE__) . '/google-api-php-client/src/contrib/Google_AnalyticsService.php';

        session_start();

        $gaFetcher = new GaFetcher;
        $gaFetcher->application_name = 'Reader Happiness Indicator';
        $gaFetcher->client_id = '108350372048.apps.googleusercontent.com';
        $gaFetcher->client_secret = 'zh7QvzhRFSG8GSvlvd2RDIdn';
        $gaFetcher->redirect_uri = 'http://localphpsvr.com/demola/fetchdata/main.php';
        $gaFetcher->developer_key = 'AIzaSyCRNjROyNkZ2QmSoIkaXcJGsNoYbnJskYw';
        $gaFetcher->access_type = 'offline';

//        $gaFetcher->application_name = 'Reader Happiness Indicator';
//        $gaFetcher->client_id = '108350372048-l6qvnu9uf56u3j64noakj3qgoo4usaqf.apps.googleusercontent.com';
//        $gaFetcher->client_secret = 'EpAKLLeTSk6xUjJ0zvCk0PkS';
//        $gaFetcher->redirect_uri = 'http://demolavk02.mediacluster.fi/fetch-ga-data/main.php';
//        $gaFetcher->developer_key = 'AIzaSyCRNjROyNkZ2QmSoIkaXcJGsNoYbnJskYw';
//        $gaFetcher->access_type = 'offline';

        $client = $gaFetcher->getGoogleClient();

        $service = $gaFetcher->getGoogleService();

        if (isset($_GET['code'])) {
            $client->authenticate();
            $_SESSION['token'] = $client->getAccessToken();
            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
        }

        if (isset($_SESSION['token'])) {
            $client->setAccessToken($_SESSION['token']);
        }

        try {
            $gaFetcher->getData();
        } catch (Exception $ex) {
            //expired access token, get refresh token from db
            $tokenObj = new Token();
            $refreshToken = null;
            $result = $tokenObj->getTokenByType(Token::TYPE_REFRESH);
            if (!empty($result)) {
                $refreshToken = $result[0]['token'];
            }
            //if access refresh access token from db is not empty
            if (strlen($refreshToken)) {
                $client->refreshToken($refreshToken);
                $_SESSION['token'] = $client->getAccessToken();
                $gaFetcher->getData();
            } else {
                //if we don't have access token from db
                //we have do authenticate by access through web browser
                $authUrl = $client->createAuthUrl();
                echo "<a href='$authUrl'>Connect Me!</a>";
            }
        }

        $time_end = microtime();
        $time = $time_end - $time_start;

        echo $time;
        ?>

    </body>
</html>